package reduce.fz.com.reduce.activity;

import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.ImageDisplayPagerAdapter;

public class ImageDisplayActivity extends BaseActivity {
    public final static String PARAM_IMAGE_PATH_LIST = "image_path_list";
    public final static String PARAM_IMAGE_POSITION = "position";

    private ViewPager mImageDisplayViewPager;
    private ImageDisplayPagerAdapter mImageDisplayPagerAdapter;
    public List<View> dotViewsList;
    public LinearLayout mDotContainerLinearLayout;
    private List<String> mImagePathList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.onCreate(savedInstanceState);
        mImagePathList = getIntent().getStringArrayListExtra(PARAM_IMAGE_PATH_LIST);
        if (mImagePathList == null || mImagePathList.isEmpty()) {//结束了
            finish();
            return;
        }
        int position = getIntent().getIntExtra(PARAM_IMAGE_POSITION, 0);
        String userName = getIntent().getStringExtra("nickname");

        dotViewsList = new ArrayList<>();
        mImageDisplayViewPager = (ViewPager) findViewById(R.id.version_image_container);
        mDotContainerLinearLayout = (LinearLayout) findViewById(R.id.image_viewpager_dot_list);
        mImageDisplayPagerAdapter = new ImageDisplayPagerAdapter(this, userName, new ImageDisplayPagerAdapter.OnCloseImageDisplayListener() {
            @Override
            public void onCloseVersionDisplay() {
                finish();
            }
        });
        mImageDisplayViewPager.setAdapter(mImageDisplayPagerAdapter);

        mImageDisplayViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0, len = dotViewsList.size(); i < len; i++) {
                    if (i == position) {//current
                        dotViewsList.get(i).setBackgroundResource(R.drawable.dot_item_current);
                    } else {
                        dotViewsList.get(i).setBackgroundResource(R.drawable.dot_item);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_DRAGGING) {//在拖拽
                }
            }
        });


        int wh = getResources().getDimensionPixelSize(R.dimen.dot_item);
        int size = mImagePathList.size();
        if( size == 1 ){
            mDotContainerLinearLayout.setVisibility(View.GONE);
        } else {
            mDotContainerLinearLayout.setVisibility(View.VISIBLE);
            for (int i = 0; i < size; i++) {
                View dot = new View(mDotContainerLinearLayout.getContext());
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(wh, wh);
                layoutParams1.setMargins(wh / 3, 0, wh / 3, 0);
                dot.setLayoutParams(layoutParams1);
                if (i > 0) {
                    dot.setBackgroundResource(R.drawable.dot_item);
                } else {
                    dot.setBackgroundResource(R.drawable.dot_item_current);
                }
                mDotContainerLinearLayout.addView(dot);
                dotViewsList.add(dot);
            }
        }

        mImageDisplayPagerAdapter.replaceVersionDisplay(mImagePathList);
        mImageDisplayViewPager.setCurrentItem(position);


    }

    @Override
    public int getContentRid() {
        return R.layout.activity_image_display;
    }

    @Override
    public void initWidget() {

    }

    @Override
    public void initListener() {

    }

}
